import { login, logout } from "@/api/user";
import AdminController from "@/api/controller/AdminController";
// import { jsonInBlacklist } from "@/api/jwt";
import router, { getUserRouter, resetRouter } from "@/router/index";
// import Vue from "vue";
import { getRolesList, cutover } from "../../api/systemManage/userManage";
import dbl from "@/utils/dbl";
import permission from "./permission";

export const user = {
  namespaced: true,
  state: {
    userInfo: "",
    rolesInfo: "",
    token: "",
    rolesList: "",
    orgInfo: "",
    role: null,
    levelID: null,
  },
  mutations: {
    setRolesList(state, rolesList) {
      state.rolesList = rolesList;
      // console.log('state.userInfo',state.userInfo)
      state.userInfo = { ...dbl.get("userInfo"), realName: rolesList.realName };
      // console.log('setRolesList', rolesList)
    },
    setRolesInfo(state, rolesInfo) {
      // sessionStorage.clear();
      state.rolesInfo = rolesInfo;
      dbl.set("rolesInfo", rolesInfo);
    },
    setUserInfo(state, userInfo) {
      // 这里的 `state` 对象是模块的局部状态
      state.userInfo = userInfo;
      dbl.set("userInfo", userInfo);
    },
    setOrgInfo(state, orgInfo) {
      // 这里的 `state` 对象是模块的局部状态
      state.orgInfo = orgInfo;
      dbl.set("orgInfo", orgInfo);
    },
    setRole(state, role) {
      // 这里的 `state` 对象是模块的局部状态
      state.role = role;
      dbl.set("role", role);
    },
    setToken(state, token) {
      // 这里的 `state` 对象是模块的局部状态
      state.token = token;
      dbl.set("token", token);
    },
    // NeedInit(state) {
    //   state.userInfo = {};
    //   state.token = "";
    //   sessionStorage.clear();
    //   router.push({ name: "init", replace: true });
    // },
    LoginOut(state) {
      sessionStorage.clear();
      state.userInfo = "";
      state.token = "";
      state.rolesList = "";
      // utils.isWxwork
      // ? window.location.replace(utils.oauth2Url)
      // : window.location.replace(utils.saomaUrl);
      setTimeout(() => {
        router.push({ name: "login", replace: true });
        window.location.reload();
      }, 1000);

      // window.location.href = process.env.VUE_APP_BASE_URL;
      // router.push({ name: "login" });
    },
    ResetUserInfo(state, userInfo = {}) {
      state.userInfo = { ...state.userInfo, ...userInfo };
    },
  },
  actions: {
    // 正常登陆
    async LoginIn({ commit, dispatch, rootGetters, getters }, loginInfo) {
      let res = await login(loginInfo);
      // console.log("login res", res);
      if (res.code == 0) {
        sessionStorage.clear();
        let rolesInfo = "";
        rolesInfo = (res.data.org?.name || "") + (res.data.role?.name || "");
        commit("setUserInfo", res.data.user);
        commit("setToken", res.data.token);
        commit("setRolesInfo", rolesInfo);
        commit("setOrgInfo", res.data.org);
        commit("setRole", res.data.role);
        // Vue.ls.set("userInfo", res.data.user, 1 * 24 * 60 * 60 * 1000);
        // Vue.ls.set("token", res.data.token, 1 * 24 * 60 * 60 * 1000);
        // Vue.ls.set("orgInfo", res.data.org, 1 * 24 * 60 * 60 * 1000);
        // Vue.ls.set("rolesInfo", rolesInfo, 1 * 24 * 60 * 60 * 1000);
        // Vue.ls.set("role", res.data.role, 1 * 24 * 60 * 60 * 1000);
        // state.role = res.data.role;
        await dispatch("getRolesList");
        await dispatch("router/SetAsyncRouter", {}, { root: true });
        await dispatch("dictionary/getFindByLevel", {}, { root: true });
        const asyncRouters = rootGetters["router/asyncRouters"];
        router.addRoutes(asyncRouters);

        // const redirect = router.history.current.query.redirect
        // console.log(redirect)
        // if (redirect) {
        //     router.push({ path: redirect })
        // } else {
        router.push({
          name: getters["userInfo"]?.authority?.defaultRouter,
          replace: true,
        });
        // window.location.reload()
        // }
        return true;
      }
    },
    // 重新获取用户信息
    async getMenu({ commit, dispatch, rootGetters, getters }, data) {
        let res = await AdminController.getInfo({ data });
        let body = res.data;
        if (res?.code == 0) {
          let userRouters = getUserRouter(body ? body.menuPermission : "");
          commit("setUserInfo", body);
          await dispatch("permission/setRouters", userRouters, { root: true });
          // commit("permission/SET_ROUTERS", userRouters);
          
          resetRouter();
        }
      
    },
    // 退出登陆
    async LoginOut({ commit }, type) {
      if (type && type == 1) {
        const res = await logout();
        if (res.code == 0) {
          commit("LoginOut");
        }
      } else {
        commit("LoginOut");
      }
    },
    // 角色列表获取
    async getRolesList({ commit }) {
      const res = await getRolesList({});
      let { code, data } = res;
      if (code == 0) {
        commit("setRolesList", data);
      }
    },
  },
  getters: {
    getRole(state) {
      return state.role ?? dbl.get("role");
    },
    userInfo(state) {
      let userInfo = state.userInfo || dbl.get("userInfo") || {};
      userInfo.authority = {
        defaultRouter: "dashboard",
      };
      return userInfo;
    },
    rolesInfo(state) {
      return state.rolesInfo || dbl.get("rolesInfo") || "未绑定角色";
    },
    orgInfo(state) {
      return state.orgInfo || dbl.get("orgInfo");
    },
    token(state) {
      return state.token || dbl.get("token");
    },
    detailsInfo(state) {
      return state.detailsInfo;
    },
    rolesList(state) {
      return state.rolesList || [];
    },
  },
};
